package com.ktjy;

import java.lang.reflect.Field;

public class Demo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //1.解剖.class文件的基本信息
        Class clz= Person.class;

        //获取这个类中的属性
        //1.获取所有属性(只会返回被public修饰的)
        Field[] fields = clz.getFields();
        for (Field field : fields) {
            System.out.println(field.getType().getSimpleName()+":"+field.getName());
        }
        System.out.println("--------------------------------------------------------");
        //2.获取所有属性(不管是什么访问修饰符)
        Field[] declaredFields = clz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getType().getSimpleName()+":"+declaredField.getName());
        }
        System.out.println("------------------------------------");
        //3.获取指定的属性(只能拿呗public修饰的属性)
        Field age = clz.getField("name");
        System.out.println(age.getName());
        System.out.println("------------------------------------");
        //4.获取指定的属性(不管访问修饰是什么)
        Field age1 = clz.getDeclaredField("age");
        System.out.println(age1.getName());
    }
}
